const express = require('express');
const router = express.Router();
const db = require('../../../../util/dbConfig/dbConfig')
const dateReplace = require('../../../../public/javascripts/timeFormat')
//修改
router.post('/',async (req,res) => {
    //鉴权
    if(req.session.userInfo.admin !==1 || !req.session.userInfo){
        return res.send({
            code: 3,
            msg: '非管理员权限或未登录'
        })
    }
    let {
        ID,  //绒质信息编号
        sheepId,                        //羊只编号
        specimen,                       //样品编号
        //上面参数为索引参数，不可以修改
        //下面参数为修改参数，可以修改必须有值
        diameter,                       //直径
        diameter_difference,            //直径标准差
        length,                         //长度
        length_length,                  //长度标准差
        length_codefficient,            //长度变异系数
        measurement_number,             //测量数
        measurement_date,               //测量日期
        remake,                         //备注

    } = req.body
    //时间格式修改
    measurement_date = await dateReplace(measurement_date,res)
    //判断三个参数是否存在和是否有值
    if(!ID || !sheepId || !specimen){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    //修改的参数
    let arr = [
        diameter,                       //直径
        diameter_difference,            //直径标准差
        length,                         //长度
        length_length,                  //长度标准差
        length_codefficient,            //长度变异系数
        measurement_number,             //测量数
        measurement_date,               //测量日期
        remake,                         //备注
    ]
    //判断修改参数是否有值
    for(let i = 0;i < arr.length-1 ; i++){
        if(arr[i] === null){
            return res.send({
                code: 1,
                msg: '不允许修改的数据无内容'
            })
        }
        if(arr[i].type === String){
            arr[i].type = Number
        }
    }
    try{
        let upSql = `update sheepftne 
        set DIAMETER=?,
        D_DIFFERENCE=?,
        LENGTH=?,
        L_DIFFERENCE=?,
        L_CODEFFICIENT=?,
        MEASUREMENT_NUMBER=?,
        MEASUREMENT_DATE=?,
        REMARK=? where ID=${ID} and SHEEP_ID=${sheepId} and SPECIMEN=${specimen}`
        let result = await db.SySqlConnect(upSql,arr)
        if(result.changedRows !==0){
            return res.send({
                code: 0,
                msg: '修改成功'
            })
        }else {
            return res.send({
                code: 2,
                msg: '修改失败'
            })
        }
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
